<template>
  <div>
    <el-table
      v-if="tableData"
      :data="
        tableData.filter(
          (data) =>
            !search || data.uname.toLowerCase().includes(search.toLowerCase())
        )
      "
      border
      style="width: 100%"
    >
      <el-table-column type="index" prop="id" width="30"> </el-table-column>
      <el-table-column prop="uname" label="用户名" width="200" sortable></el-table-column>
      <el-table-column prop="uemail" label="邮箱" width="200"></el-table-column>
      <el-table-column prop="ugender" label="性别" width="60"></el-table-column>
      <el-table-column prop="ubirthday" label="生日" width="200" sortable></el-table-column>
      <el-table-column prop="uphone" label="电话号码">
        <template slot="header" slot-scope="{}">
          <el-input v-model="search" size="mini" placeholder="输入用户名搜索" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-row>
      <el-col :span="20" :offset="4">
        <el-pagination
          layout="prev, pager, next, jumper, total, sizes"
          background
          prev-text="prev"
          next-text="next"
          :page-size="size"
          :current-page="pageNow"
          :page-sizes="[5, 10, 15, 20]"
          @current-change="findPage"
          @size-change="findSize"
          :total="total"
        >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { queryUserList } from "@/network/admin";

export default {
  name: "houseInfo",
  data() {
    return {
      tableData: [
        {
          uid:'U0001',
          uname:'测试',
          upwd:'test',
          uemail:'438484791@qq.com',
          ugender:'女',
          uphone:'15269553075',
          ubirthday:'2000-09-02',
          uimg:'1.jpg',
        },
      ],
      search: "",
      total: 0,
      size: 5,
      pageNow: 0,
    };
  },
  created() {
    queryUserList().then((res) => {
      // console.log(res);
      console.log(res.users);
      this.tableData = res.users;
      this.total = res.total;
    });
  },
  methods: {
    // 处理
    findSize(size) {
      // console.log(size);
      this.size = size;
      this.findAllTableData((this.page - 1) * size, size);
    },
    // 处理分页的方法
    findPage(page) {
      console.log("page", page);
      this.pageNow = page;
      console.log((page - 1) * this.size);
      this.findAllTableData((page - 1) * this.size, this.size);
    },
    findAllTableData(page, size) {
      console.log("findAllTableData", page, size);
      page = page ? page : this.pageNow;
      size = size ? size : this.size;
      // 记住传参
      queryUserList(page, size).then((res) => {
        console.log(res.users);
        this.tableData = res.users;
        this.total = res.total;
      });
    },
  },
};
</script>

<style>
.transition-box {
  margin-bottom: 10px;
  width: 100%;
  height: 500px;
  border-radius: 4px;
  padding: 40px 20px;
  box-sizing: border-box;
  margin-right: 20px;
}
</style>